package com.csoa.community.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;

import com.collect.community.R;
import com.csoa.community.adapter.AreaAdapter;
import com.csoa.community.bean.AreaBean;
import com.csoa.community.bean.IssueGroupBean;
import com.csoa.community.db.IssueGroupDB;
import com.csoa.community.db.YFInfoDB;
import com.csoa.community.singletons.GlobalVars;
import com.csoa.community.utils.StringUtils;
import com.csoa.community.widget.NarView;

public class IssueGroupActivity extends BaseActivity {
	ListView lv;
	private Spinner province_spinner;
	private Spinner city_spinner;
	private Spinner county_spinner;
	private Integer provinceId, cityId;
	// private EditText display;
	private String strProvince, strCity, strCounty;

	private ArrayAdapter<CharSequence> province_adapter;
	private ArrayAdapter<CharSequence> city_adapter;
	private ArrayAdapter<CharSequence> county_adapter;
	private ListView mListArea;
	private List<AreaBean> mDataArea;
	private AreaAdapter mAreaAdapter;
	private EditText etUnitIssue;
	private EditText etPrincipalIssue;
	private EditText etWriterIssue;
	private CheckBox checkZhiWu;
	private CheckBox checkShuiShengZhiWu;
	private CheckBox checkDongWu;
	private CheckBox checkWeiShengWu;
	private CheckBox checkHaiYangShengWu;
	private IssueGroupBean mIssueGroupBean;
	private ListView mListValley;
	private ArrayList<AreaBean> mDataValley;
	private AreaAdapter mValleyAdapter;

	@Override
	public void initUI() {
		setContentView(R.layout.issue_group);
		mIssueGroupBean = (IssueGroupBean) this.getIntent()
				.getSerializableExtra("IssueGroup");

		this.hiddenNar(R.id.nav_setting);
		etUnitIssue = (EditText) this.findViewById(R.id.etUnitIssue);
		if (mIssueGroupBean != null) {
			etUnitIssue.setText(mIssueGroupBean.unit);
		}
		etPrincipalIssue = (EditText) this.findViewById(R.id.etPrincipalIssue);
		if (mIssueGroupBean != null) {
			etPrincipalIssue.setText(mIssueGroupBean.principal);
		}
		etWriterIssue = (EditText) this.findViewById(R.id.etWriterIssue);
		if (mIssueGroupBean != null) {
			etWriterIssue.setText(mIssueGroupBean.writer);
		}
		checkZhiWu = (CheckBox) this.findViewById(R.id.checkPhytologyIssue);
		checkShuiShengZhiWu = (CheckBox) this
				.findViewById(R.id.checkWaterPhytologyIssue);
		checkDongWu = (CheckBox) this.findViewById(R.id.checkZoologyIssue);
		checkWeiShengWu = (CheckBox) this.findViewById(R.id.checkMicrobeIssue);
		checkHaiYangShengWu = (CheckBox) this
				.findViewById(R.id.checkHalobiosIssue);
		boolean[] isChecks = { true, false, false, false, false, };
		if (mIssueGroupBean != null) {
			String[] types = mIssueGroupBean.classification.split(",");
			for (int i = 0; i < types.length; i++) {
				if ((types[i]).equals("1")) {
					isChecks[i] = true;
				} else {
					isChecks[i] = false;
				}
			}
			checkZhiWu.setChecked(isChecks[0]);
			checkShuiShengZhiWu.setChecked(isChecks[1]);
			checkDongWu.setChecked(isChecks[2]);
			checkWeiShengWu.setChecked(isChecks[3]);
			checkHaiYangShengWu.setChecked(isChecks[4]);
		}

		this.findViewById(R.id.btnSaveIssue).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {

						String unit = etUnitIssue.getText().toString();
						String principal = etPrincipalIssue.getText()
								.toString();
						String writer = etWriterIssue.getText().toString();
						if (StringUtils.isEmpty(unit)) {
							IssueGroupActivity.this.showToast("请填写所属单位");
							return;
						}
						if (StringUtils.isEmpty(principal)) {
							IssueGroupActivity.this.showToast("请填写负责人");
							return;
						}
						if (StringUtils.isEmpty(writer)) {
							IssueGroupActivity.this.showToast("请填写填报人");
							return;
						}
						if (mDataArea == null || mDataArea.size() == 0) {
							IssueGroupActivity.this.showToast("您还没有添加调查区域或流域");
							return;
						}
						if (!checkZhiWu.isChecked()
								&& !checkShuiShengZhiWu.isChecked()
								&& !checkDongWu.isChecked()
								&& !checkWeiShengWu.isChecked()
								&& !checkHaiYangShengWu.isChecked()) {
							IssueGroupActivity.this.showToast("您还没有设置调查分类");
							return;
						}

						IssueGroupBean issueGroup = new IssueGroupBean();
						issueGroup.unit = unit;
						issueGroup.principal = principal;
						issueGroup.writer = writer;
						StringBuffer strArea = new StringBuffer();
						for (int i = 0; i < mDataArea.size(); i++) {
							strArea.append(mDataArea.get(i).areaName + ",");
						}
						issueGroup.area = strArea.toString().substring(0,
								strArea.toString().length() - 1);
						issueGroup.classification = (checkZhiWu.isChecked() ? "1"
								: "0")
								+ ","
								+ (checkShuiShengZhiWu.isChecked() ? "1" : "0")
								+ ","
								+ (checkDongWu.isChecked() ? "1" : "0")
								+ ","
								+ (checkWeiShengWu.isChecked() ? "1" : "0")
								+ ","
								+ (checkHaiYangShengWu.isChecked() ? "1" : "0");

						IssueGroupBean dbIssueGroup = IssueGroupDB.query();
						if (dbIssueGroup == null) {
							IssueGroupDB.insert(issueGroup);
						} else {
							IssueGroupDB.update(issueGroup, dbIssueGroup.id);
							// YFInfoDB.delete();
						}
						showToast("课题组信息保存成功");
						Intent intent = new Intent(IssueGroupActivity.this,
								MainActivity.class);
						startActivity(intent);
						finish();
					}
				});
		this.findViewById(R.id.btnAddArea).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						if (!StringUtils.isEmpty(strCounty)) {
							AreaBean areaBean = new AreaBean();
							areaBean.areaName = strCounty;
							areaBean.isChecked = false;
							if (!mDataArea.contains(areaBean)) {
								mDataArea.add(areaBean);
								mAreaAdapter.notifyDataSetChanged();
							} else {
								IssueGroupActivity.this.showToast("您已经添加过该地区");
							}
						}
					}
				});
		this.findViewById(R.id.btnAddVallery).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						if (!StringUtils.isEmpty(strCounty)) {
							AreaBean areaBean = new AreaBean();
							areaBean.areaName = strCounty;
							areaBean.isChecked = false;
							if (!mDataArea.contains(areaBean)) {
								mDataArea.add(areaBean);
								mAreaAdapter.notifyDataSetChanged();
							} else {
								IssueGroupActivity.this.showToast("您已经添加过该地区");
							}
						}
					}
				});
		this.findViewById(R.id.btnBackIssue).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						finish();
					}
				});

		this.findViewById(R.id.btnCancelIssue).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						if (mIssueGroupBean != null) {
							Intent intent = new Intent(IssueGroupActivity.this,
									MainActivity.class);
							startActivity(intent);
							finish();
						} else {
							finish();
						}
					}
				});

		this.findViewById(R.id.btnDelArea).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {

						delAreaOrVallery(mDataArea, mAreaAdapter);
					}
				});

		this.findViewById(R.id.btnDelVallery).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						delAreaOrVallery(mDataValley, mValleyAdapter);
					}
				});

		mListArea = (ListView) this.findViewById(R.id.listAddingAreaIssue);
		mDataArea = new ArrayList<AreaBean>();
		mAreaAdapter = new AreaAdapter(this, mDataArea);
		mListArea.setAdapter(mAreaAdapter);

		mListValley = (ListView) this.findViewById(R.id.listAddingValleyIssue);
		mDataValley = new ArrayList<AreaBean>();
		mValleyAdapter = new AreaAdapter(this, mDataValley);
		mListValley.setAdapter(mValleyAdapter);

	}

	/**
	 * 删除区域或流域
	 * 
	 * @param mData
	 * @param mAdapter
	 */
	protected void delAreaOrVallery(List<AreaBean> mData, AreaAdapter mAdapter) {
		List<AreaBean> checkedData = new ArrayList<AreaBean>();
		for (int i = 0; i < mData.size(); i++) {
			if (mData.get(i).isChecked) {
				checkedData.add(mDataArea.get(i));
			}
		}
		if (checkedData.size() == 0) {
			IssueGroupActivity.this.showToast("请选择要删除的区域");
			return;
		}

		IssueGroupBean issue = IssueGroupDB.query();
		if (issue != null) {

			List<AreaBean> dbContailData = new ArrayList<AreaBean>();
			convertStringToList(dbContailData, issue.area);

			for (int i = 0; i < checkedData.size(); i++) {
				boolean isContail = dbContailData.contains(checkedData.get(i));
				if (isContail) {
					IssueGroupActivity.this.showToast("您选择的区域已经存在记录,不能删除");
					return;
				}
			}

		}

		if (mData != null && mData.size() != 0) {
			mData.removeAll(checkedData);
		}
		mAdapter.notifyDataSetChanged();
	}

	private void loadSpinner() {
		// display = (EditText) findViewById(R.id.display_edit);
		province_spinner = (Spinner) findViewById(R.id.province_spinner);

		province_adapter = ArrayAdapter.createFromResource(this,
				R.array.province_item, android.R.layout.simple_spinner_item);
		province_adapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		province_spinner.setAdapter(province_adapter);
		province_spinner.setPrompt("请选择省份");
		// select(province_spinner, province_adapter, R.array.province_item);
		province_spinner
				.setOnItemSelectedListener(new OnItemSelectedListener() {
					@Override
					public void onItemSelected(AdapterView<?> arg0, View arg1,
							int arg2, long arg3) {
						provinceId = province_spinner.getSelectedItemPosition();
						strProvince = province_spinner.getSelectedItem()
								.toString();
						city_spinner = (Spinner) findViewById(R.id.city_spinner);
						if (true) {
							Log.v("test", "province: "
									+ province_spinner.getSelectedItem()
											.toString() + provinceId.toString());
							county_spinner = (Spinner) findViewById(R.id.county_spinner);
							city_spinner = (Spinner) findViewById(R.id.city_spinner);
							city_spinner.setPrompt("请选择城市");
							select(city_spinner, city_adapter,
									GlobalVars.getInst().city[provinceId]);
							city_spinner
									.setOnItemSelectedListener(new OnItemSelectedListener() {

										@Override
										public void onItemSelected(
												AdapterView<?> arg0, View arg1,
												int arg2, long arg3) {
											cityId = city_spinner
													.getSelectedItemPosition();
											strCity = city_spinner
													.getSelectedItem()
													.toString();
											Log.v("test", "city: "
													+ city_spinner
															.getSelectedItem()
															.toString()
													+ cityId.toString());
											if (true) {
												county_spinner = (Spinner) findViewById(R.id.county_spinner);
												county_spinner
														.setPrompt("请选择县区");
												switch (provinceId) {
												case 0:
													select(county_spinner,
															county_adapter,
															GlobalVars
																	.getInst().countyOfBeiJing[cityId]);
													break;
												case 1:
													select(county_spinner,
															county_adapter,
															GlobalVars
																	.getInst().countyOfTianJing[cityId]);
													break;
												case 2:
													select(county_spinner,
															county_adapter,
															GlobalVars
																	.getInst().countyOfHeBei[cityId]);
													break;
												case 3:
													select(county_spinner,
															county_adapter,
															GlobalVars
																	.getInst().countyOfShanXi1[cityId]);
													break;
												case 4:
													select(county_spinner,
															county_adapter,
															GlobalVars
																	.getInst().countyOfNeiMengGu[cityId]);
													break;
												case 5:
													select(county_spinner,
															county_adapter,
															GlobalVars
																	.getInst().countyOfLiaoNing[cityId]);
													break;
												case 6:
													select(county_spinner,
															county_adapter,
															GlobalVars
																	.getInst().countyOfJiLin[cityId]);
													break;
												case 7:
													select(county_spinner,
															county_adapter,
															GlobalVars
																	.getInst().countyOfHeiLongJiang[cityId]);
													break;
												case 8:
													select(county_spinner,
															county_adapter,
															GlobalVars
																	.getInst().countyOfShangHai[cityId]);
													break;
												case 9:
													select(county_spinner,
															county_adapter,
															GlobalVars
																	.getInst().countyOfJiangSu[cityId]);
													break;
												case 10:
													select(county_spinner,
															county_adapter,
															GlobalVars
																	.getInst().countyOfZheJiang[cityId]);
													break;
												case 11:
													select(county_spinner,
															county_adapter,
															GlobalVars
																	.getInst().countyOfAnHui[cityId]);
													break;
												case 12:
													select(county_spinner,
															county_adapter,
															GlobalVars
																	.getInst().countyOfFuJian[cityId]);
													break;
												case 13:
													select(county_spinner,
															county_adapter,
															GlobalVars
																	.getInst().countyOfJiangXi[cityId]);
													break;
												case 14:
													select(county_spinner,
															county_adapter,
															GlobalVars
																	.getInst().countyOfShanDong[cityId]);
													break;
												case 15:
													select(county_spinner,
															county_adapter,
															GlobalVars
																	.getInst().countyOfHeNan[cityId]);
													break;
												case 16:
													select(county_spinner,
															county_adapter,
															GlobalVars
																	.getInst().countyOfHuBei[cityId]);
													break;
												case 17:
													select(county_spinner,
															county_adapter,
															GlobalVars
																	.getInst().countyOfHuNan[cityId]);
													break;
												case 18:
													select(county_spinner,
															county_adapter,
															GlobalVars
																	.getInst().countyOfGuangDong[cityId]);
													break;
												case 19:
													select(county_spinner,
															county_adapter,
															GlobalVars
																	.getInst().countyOfGuangXi[cityId]);
													break;
												case 20:
													select(county_spinner,
															county_adapter,
															GlobalVars
																	.getInst().countyOfHaiNan[cityId]);
													break;
												case 21:
													select(county_spinner,
															county_adapter,
															GlobalVars
																	.getInst().countyOfChongQing[cityId]);
													break;
												case 22:
													select(county_spinner,
															county_adapter,
															GlobalVars
																	.getInst().countyOfSiChuan[cityId]);
													break;
												case 23:
													select(county_spinner,
															county_adapter,
															GlobalVars
																	.getInst().countyOfGuiZhou[cityId]);
													break;
												case 24:
													select(county_spinner,
															county_adapter,
															GlobalVars
																	.getInst().countyOfYunNan[cityId]);
													break;
												case 25:
													select(county_spinner,
															county_adapter,
															GlobalVars
																	.getInst().countyOfXiZang[cityId]);
													break;
												case 26:
													select(county_spinner,
															county_adapter,
															GlobalVars
																	.getInst().countyOfShanXi2[cityId]);
													break;
												case 27:
													select(county_spinner,
															county_adapter,
															GlobalVars
																	.getInst().countyOfGanSu[cityId]);
													break;
												case 28:
													select(county_spinner,
															county_adapter,
															GlobalVars
																	.getInst().countyOfQingHai[cityId]);
													break;
												case 29:
													select(county_spinner,
															county_adapter,
															GlobalVars
																	.getInst().countyOfNingXia[cityId]);
													break;
												case 30:
													select(county_spinner,
															county_adapter,
															GlobalVars
																	.getInst().countyOfXinJiang[cityId]);
													break;
												case 31:
													select(county_spinner,
															county_adapter,
															GlobalVars
																	.getInst().countyOfHongKong[cityId]);
													break;
												case 32:
													select(county_spinner,
															county_adapter,
															GlobalVars
																	.getInst().countyOfAoMen[cityId]);
													break;
												case 33:
													select(county_spinner,
															county_adapter,
															GlobalVars
																	.getInst().countyOfTaiWan[cityId]);
													break;
												default:
													break;
												}

												county_spinner
														.setOnItemSelectedListener(new OnItemSelectedListener() {

															@Override
															public void onItemSelected(
																	AdapterView<?> arg0,
																	View arg1,
																	int arg2,
																	long arg3) {
																strCounty = county_spinner
																		.getSelectedItem()
																		.toString();
																// display.setText(strProvince+"-"+strCity+"-"+strCounty);
															}

															@Override
															public void onNothingSelected(
																	AdapterView<?> arg0) {

															}

														});
											}
										}

										@Override
										public void onNothingSelected(
												AdapterView<?> arg0) {
											// TODO Auto-generated method stub

										}

									});
						}
					}

					@Override
					public void onNothingSelected(AdapterView<?> arg0) {

					}
				});

	}

	private void select(Spinner spin, ArrayAdapter<CharSequence> adapter,
			int arry) {
		adapter = ArrayAdapter.createFromResource(this, arry,
				android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spin.setAdapter(adapter);
	}

	@Override
	public void initData() {
		if (mIssueGroupBean != null) {
			convertStringToList(mDataArea, mIssueGroupBean.area);
			// convertStringToList(mDataValley, mIssueGroupBean.valley);
		}
		loadSpinner();
	}

	private void convertStringToList(List<AreaBean> dataArea, String strData) {
		dataArea.clear();
		String[] types = strData.split(",");
		for (int i = 0; i < types.length; i++) {
			AreaBean areaBean = new AreaBean();
			areaBean.areaName = types[i];
			areaBean.isChecked = false;
			dataArea.add(areaBean);
		}
	}

}
